package com.example.demo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.example.module_motocircle.adpater.PostViewHolder
import com.example.module_motocircle.databinding.LayoutPostItemBinding
import com.example.module_motocircle.model.PostItem
import com.google.android.exoplayer2.util.MimeTypes

class DemoPostListAdapter(
    private val onItemClick: ((PostItem, Int) -> Unit)? = null,
    private val onUserClick: ((PostItem) -> Unit)? = null,
//    private val onImageClickWithThumbnail: ((Int, Int, List<Image>, List<Info>) -> Unit)? = null,
    private val onShareClick: ((PostItem) -> Unit)? = null,
    private val onCommentsClick: ((PostItem) -> Unit)? = null,
    private val onLikeClick: ((PostItem, Int) -> Unit)? = null,
    private val onMoreActionClick: ((PostItem, Int) -> Unit)? = null,
    private val onLikeViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val onCollectionViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val onFollowViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val showAuthorLabel: Boolean = true,
    // 是否显示关系
    private val showRelationState: Boolean = true,
) : PagingDataAdapter<PostItem, PostViewHolder>(POST_COMPARATOR) {

    private val TAG = "post-video"
    private lateinit var mContext: Context

    companion object {
        private val POST_COMPARATOR = object : DiffUtil.ItemCallback<PostItem>() {
            override fun areItemsTheSame(oldItem: PostItem, newItem: PostItem): Boolean =
                oldItem.posts_id == newItem.posts_id

            override fun areContentsTheSame(oldItem: PostItem, newItem: PostItem): Boolean {
                return oldItem == newItem
            }


        }
    }

    enum class PostType(val value: Int) {
        Pic(1), Video(2)
    }


    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<LayoutPostItemBinding>(holder.itemView)
        val theItem = getItem(position)
        if (theItem != null) {
            theItem.needShowFollowState = showRelationState
            //设置图片
            holder.setMaxLines(2)
            holder.initView(theItem, position)
            binding?.post = theItem
            binding?.holder = holder
            binding?.lFollowState?.isSelected = theItem.hasFollowed()
        }
    }

    override fun getItemViewType(position: Int): Int {
        val post = getItem(position) ?: return PostType.Pic.value
        post.image ?: return PostType.Pic.value
        if (post.image.size == 1) {
            return if (MimeTypes.isVideo(post.image[0].contentType)) {
                PostType.Video.value
            } else {
                PostType.Pic.value
            }
        }
        return PostType.Pic.value

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val binding = LayoutPostItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return PostViewHolder(
            onItemClick = onItemClick,
            onUserClick = onUserClick,
//            onImageClickWithThumbnail = onImageClickWithThumbnail,
            onShareClick = onShareClick,
            onCommentsClick = onCommentsClick,
            onLikeClick = onLikeClick,
            onMoreActionClick = onMoreActionClick,
            onLikeViewClick = onLikeViewClick,
            onCollectionViewClick = onCollectionViewClick,
            onFollowViewClick = onFollowViewClick,
            mBinding = binding)
    }

    override fun onBindViewHolder(
        holder: PostViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position)
        } else {
            //不更新图片
            val binding = DataBindingUtil.getBinding<LayoutPostItemBinding>(holder.itemView)
            val theItem = getItem(position)
            if (theItem != null) {
                theItem.needShowFollowState = showRelationState
                binding?.post = theItem
                binding?.lFollowState?.isSelected = theItem.hasFollowed()
            }
        }
    }

    //获取数据源
    fun getPostItem(position: Int): PostItem? {
        if (position < itemCount)
            return getItem(position)
        return null
    }



    //修改数据源
    fun updatePostItem(position: Int, postModel: PostItem) {
        run {
            repeat(itemCount) {
                val source = getPostItem(it)
                if (source?.posts_id == postModel.posts_id) {
                    source.apply {
                        if_like = postModel.if_like
                        if_like = postModel.if_like
                        comment_number = postModel.comment_number
                        notifyItemChanged(position, "update")
                        return@run
                    }
                }
            }
        }
    }
}